/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.spring.DAO;

import com.spring.entidades.Detalle;
import com.spring.entidades.DetallePK;
import com.spring.entidades.Pedido;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author jerick
 */
@Repository
public class DetalleDAO extends HibernateDaoSupport {
    
    @Autowired

    public DetalleDAO(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }
    
        
    public List<Detalle> buscarDetalle_pedido( String  numped){
        if (numped.length()==1)
            numped="000"+numped;
        else if (numped.length()==2)
            numped="00"+numped;
        else if (numped.length()==3)
            numped="0"+numped;
        
        List<Detalle> det=null;
        try{
            det=getHibernateTemplate().findByNamedParam("select d from Detalle d where d.detallePK.nroPedido=:param", "param", numped);//en jpql
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null,"error en el DAO-------"+ e.getMessage());
        }
            
        return det;
     }
    public String encabezado(){
        String cad = null;
        cad="<th>Item</th>";
        cad+="<th>Num Pedido Pedido</th>";
        cad+="<th>Cod Producto</th>";
        cad+="<th>Cantidad</th>";
        cad+="<th>Precio</th>";
        cad+="<th>Subtotal</th>";
        return cad;
    }
    public boolean insertardetalle(List<Detalle> det){
         boolean b=false;
     try{
         for (int i=0;i<det.size();i++){
             getHibernateTemplate().saveOrUpdate(det.get(i));
         }
         b=true;
     }
     catch(Exception e)
     {
         JOptionPane.showMessageDialog(null, e.getMessage());
     }
         return b;
     }
    
    public boolean eliminarDetalle(String itemdet,String numped)
     {
         boolean b=false;
         try{
             DetachedCriteria crit = DetachedCriteria.forClass(Detalle.class);
         crit.add(Restrictions.like("item", itemdet));
         crit.add(Restrictions.like("nroPedido", numped));
        
           getHibernateTemplate().delete(crit);                                
             //Detalle t=getHibernateTemplate().get(Detalle.class, itemdet);
             
             //getHibernateTemplate().delete(t);
             b=true;
             
         }
         catch (Exception e){
             b=false;
             
         }
         return b;
     }
    public boolean modificarDetalle(String itemdet,String numped)
     {
         boolean b=false;
         try{
             DetachedCriteria crit = DetachedCriteria.forClass(Detalle.class);
         crit.add(Restrictions.like("item", itemdet));
         crit.add(Restrictions.like("nroPedido", numped));
        
           getHibernateTemplate().update(crit);                                
             //Detalle t=getHibernateTemplate().get(Detalle.class, itemdet);
             
             //getHibernateTemplate().delete(t);
             b=true;
             
         }
         catch (Exception e){
             b=false;
             
         }
         return b;
     }
    public double obtener_total(List<Detalle> det){
        double total=0;
        for (int i=0;i<det.size();i++){
             total+=det.get(i).getCantidad()*det.get(i).getCodProd().getPrecio();
        }        
        return total;
    } 
    
}
